package com.example.bsy.pinluosong;

import android.util.Log;

import java.util.Random;

/**
 * Created by bsy on 2017/3/26.
 */
public class PinLuoSong {
    Card[] card;//牌堆
    Card[] myCards;//本人的13张牌
    Card[] firstCards = new Card[3];
    int numOfFirst;
    Card[] secondCards = new Card[5];
    int numOfSecond;
    Card[] thirdCards = new Card[5];
    int numOfThird;

    PinLuoSong() {
        card = new Card[52];
        for (int i = 0; i < 52; i++) {
            card[i] = new Card(i / 13 + 1, i % 13 + 1);
        }
        shuffle();

    }//生成牌堆并洗牌

    Card[] getMyCards() {
        Card[] cards = new Card[13];
        for (int i = 0; i < 13; i++) {
            cards[i] = new Card(card[i + 13].getSuit(), card[i + 13].getPoint());
        }
        sortStraight(cards, true);
        myCards = cards;
        return cards;
    }//获取主机牌组

    Card[] getYourCards() {
        Card[] cards = new Card[13];
        for (int i = 0; i < 13; i++) {
            cards[i] = new Card(card[i + 27].getSuit(), card[i + 27].getPoint());
        }
        sortStraight(cards, true);

        return cards;
    }//获取客户机牌组

    boolean addFirstCards(int id) {
        if (numOfFirst >= 3) {
            return false;
        } else {
            firstCards[numOfFirst] = myCards[id];
            numOfFirst++;
            return true;
        }
    }//添加头道牌

    boolean addSecondCards(int id) {
        if (numOfSecond >= 5) {
            return false;
        } else {
            secondCards[numOfSecond] = myCards[id];
            numOfSecond++;
            return true;
        }
    }//添加第二道牌

    boolean addThirdCards(int id) {
        if (numOfThird >= 5) {
            return false;
        } else {
            thirdCards[numOfThird] = myCards[id];
            numOfThird++;
            return true;
        }
    }//添加第三道牌

    boolean deleteCards(int id) {
        for (int i = 0; i < numOfFirst; i++) {
            if (myCards[id].isEqual(firstCards[i])) {
                for (int k = i; k < numOfFirst - 1; k++) {
                    firstCards[k] = firstCards[k + 1];
                }
                numOfFirst--;
                return true;
            }
        }
        for (int i = 0; i < numOfSecond; i++) {
            if (myCards[id].isEqual(secondCards[i])) {
                for (int k = i; k < numOfSecond - 1; k++) {
                    secondCards[k] = secondCards[k + 1];
                }
                numOfSecond--;
                return true;
            }
        }
        for (int i = 0; i < numOfThird; i++) {
            if (myCards[id].isEqual(thirdCards[i])) {
                for (int k = i; k < numOfThird - 1; k++) {
                    thirdCards[k] = thirdCards[k + 1];
                }
                numOfThird--;
                return true;
            }
        }
        return false;
    }//删除已放置的牌

    String toString(int score){
        if(score==0xf000000) return "至尊清龙";
        else if(score==0xe000000) return "清龙";
        else if(score==0xd000000) return "十二皇族";
        else if(score==0xc000000) return "三同花顺";
        else if(score==0xb000000) return "三炸弹";
        else if(score==0xa000000) return "全大";
        else if(score==0x9000000) return "全小";
        else if(score==0x8000000) return "全红/全黑";
        else if(score==0x7000000) return "四刻子";
        else if(score==0x6000000) return "五对一刻";
        else if(score==0x5000000) return "六对半";
        else if(score==0x4000000) return "三同花";
        else if(score==0x3000000) return "三顺子";
        else if(score>=0x0800000) return "同花顺";
        else if(score>=0x0700000) return "炸弹";
        else if(score>=0x0600000) return "葫芦";
        else if(score>=0x0500000) return "同花";
        else if(score>=0x0400000) return "顺子";
        else if(score>=0x0300000) return "刻子";
        else if(score>=0x0200000) return "两对";
        else if(score>=0x0100000) return "一对";
        else return "散牌";
    }

    int jugde(Card[] group, int mode) {
        int[] t = null;
        int[] t2 = null;
        switch (mode) {
            case 0://甩牌
                if (kingDragon(group)) {
                    return 0xf000000;
                } else if (dragon(group)) {
                    return 0xe000000;
                } else if (royal(group)) {
                    return 0xd000000;
                } else if (threeStraightFlush(group)) {
                    return 0xc000000;
                } else if (threeBomb(group)) {
                    return 0xb000000;
                } else if (allLarge(group)) {
                    return 0xa000000;
                } else if (allSmall(group)) {
                    return 0x9000000;
                } else if (allRed(group)) {
                    return 0x8000000;
                } else if (allBlack(group)) {
                    return 0x8000000;
                } else if (fourTOAK(group)) {
                    return 0x7000000;
                } else if (five(group)) {
                    return 0x6000000;
                } else if (sixAndHalf(group)) {
                    return 0x5000000;
                } else if (threeFlush(group)) {
                    return 0x4000000;
                } else if (threeStraight(group)) {
                    return 0x3000000;
                } else{
                    return 0;
                }

            case 1:
                t = pairs(group, 1);
                t2 = pairs(group, 2);
                Log.i("Head",t[0]+"  "+t2[0]);
                if (t[0] == 3) {//散牌
                    return 0x10000 * group[2].getPoint() + 0x1000 * group[1].getPoint() +  0x100 *group[0].getPoint();
                } else if (t2[0] == 1) {//对子
                    if (group[0].getPoint() < group[1].getPoint()) {
                        return 0x0100000 + 0x1000 * group[1].getPoint() + 0x100 * group[0].getPoint();
                    } else {
                        return 0x0100000 + 0x1000 * group[1].getPoint() + 0x100 * group[2].getPoint();
                    }
                } else {//克子
                    return 0x0300000 + 0x100 * group[0].getPoint();
                }


            case 2:

                int sf = sortFlush(group, false);
                if (sf == 0x5000 || sf == 0x0500 || sf == 0x0050 || sf == 0x0005) {
                    if (group[4].getPoint() - group[0].getPoint() == 4) {//同花顺
                        return 0x0800000 + group[4].getPoint();
                    } else {//同花
                        sortFlush(group, true);
                        if (group[4].getPoint() - group[0].getPoint() == 4) {
                            return 0x0800000 + group[4].getPoint();
                        } else {
                            return 0x0500000 + 0x10000 * group[4].getPoint() + 0x1000 * group[3].getPoint() + 0x100 * group[2].getPoint() + 0x10 * group[1].getPoint() + group[0].getPoint();//同花
                        }
                    }
                }

                t = pairs(group, 4);
                if (t[0] == 1) {//炸弹
                    if (group[0].getPoint() < group[1].getPoint()) {
                        return 0x0700000 + 0x10 * group[1].getPoint() + group[0].getPoint();
                    } else {
                        return 0x0700000 + 0x10 * group[1].getPoint() + group[4].getPoint();
                    }
                }
                t = pairs(group, 3);
                t2 = pairs(group, 2);
                if (t[0] == 1 && t2[0] == 1) {//葫芦
                    if (group[0].getPoint() < group[2].getPoint()) {
                        return 0x0600000 + 0x10 * group[2].getPoint() + group[0].getPoint();
                    } else {
                        return 0x0600000 + 0x10 * group[2].getPoint() + group[4].getPoint();
                    }
                } else if (t[0] == 1 && t2[0] == 0) {//克子
                    if (group[0].getPoint() == group[2].getPoint()) {
                        return 0x0300000 + 0x100 * group[0].getPoint() + 0x10 * group[4].getPoint() + group[3].getPoint();
                    } else if (group[1].getPoint() == group[3].getPoint()) {
                        return 0x0300000 + 0x100 * group[1].getPoint() + 0x10 * group[4].getPoint() + group[0].getPoint();
                    } else if (group[2].getPoint() == group[4].getPoint()) {
                        return 0x0300000 + 0x100 * group[2].getPoint() + 0x10 * group[1].getPoint() + group[0].getPoint();
                    }
                }
                if (t2[0] == 2) {//两对
                    if (group[0].getPoint() == group[1].getPoint() && group[2].getPoint() == group[3].getPoint()) {
                        return 0x0200000 + 0x100 * group[2].getPoint() + 0x10 * group[0].getPoint() + group[4].getPoint();
                    } else if (group[0].getPoint() == group[1].getPoint() && group[3].getPoint() == group[4].getPoint()) {
                        return 0x0200000 + 0x100 * group[3].getPoint() + 0x10 * group[0].getPoint() + group[2].getPoint();
                    } else if (group[1].getPoint() == group[2].getPoint() && group[3].getPoint() == group[4].getPoint()) {
                        return 0x0200000 + 0x100 * group[3].getPoint() + 0x10 * group[1].getPoint() + group[0].getPoint();
                    }
                }
                if (t2[0] == 1) {//一对
                    if (group[0].getPoint() == group[1].getPoint()) {
                        return 0x0100000 + 0x1000 * group[0].getPoint() + 0x100 * group[4].getPoint() + 0x10 * group[3].getPoint() + group[2].getPoint();
                    } else if (group[1].getPoint() == group[2].getPoint()) {
                        return 0x0100000 + 0x1000 * group[1].getPoint() + 0x100 * group[4].getPoint() + 0x10 * group[3].getPoint() + group[0].getPoint();
                    } else if (group[2].getPoint() == group[3].getPoint()) {
                        return 0x0100000 + 0x1000 * group[2].getPoint() + 0x100 * group[4].getPoint() + 0x10 * group[1].getPoint() + group[0].getPoint();
                    } else if (group[3].getPoint() == group[4].getPoint()) {
                        return 0x0100000 + 0x1000 * group[3].getPoint() + 0x100 * group[2].getPoint() + 0x10 * group[1].getPoint() + group[0].getPoint();
                    }
                }
                t = pairs(group, 1);
                if (t[0] == 5) {//散牌顺子
                    if (group[4].getPoint() - group[0].getPoint() == 4) {
                        return 0x0400000 + group[4].getPoint();
                    } else {
                        sortStraight(group, true);
                        if (group[4].getPoint() - group[0].getPoint() == 4) {
                            return 0x0400000 + group[4].getPoint();
                        } else {
                            return 0x10000 * group[4].getPoint() + 0x1000 *group[3].getPoint() + 0x100 * group[2].getPoint() + 0x10 * group[1].getPoint() + group[0].getPoint();
                        }
                    }
                }
                break;
        }
        return 0;//此处理应不可及
    }//判定逻辑

    int[] analyse(int myScore, int myScore1, int myScore2, int myScore3, int yourScore, int yourScore1, int yourScore2, int yourScore3) {
        int[] score = new int[8];
        for (int i = 0; i < 8; i++) {
            score[i] = 0;
        }
        if (myScore == yourScore && myScore != 0) {
            //平局，全为0
        } else if (myScore > yourScore) {
            switch (myScore){
                case 0xf000000:
                    score[0]=108;
                    score[4]=-108;
                    break;
                case 0xe000000:
                    score[0]=36;
                    score[4]=-36;
                    break;
                case 0xd000000:
                    score[0]=24;
                    score[4]=-24;
                    break;
                case 0xc000000:
                    score[0]=22;
                    score[4]=-22;
                    break;
                case 0xb000000:
                    score[0]=20;
                    score[4]=-20;
                    break;
                case 0xa000000:
                    score[0]=15;
                    score[4]=-15;
                    break;
                case 0x9000000:
                    score[0]=12;
                    score[4]=-12;
                    break;
                case 0x8000000:
                    score[0]=10;
                    score[4]=-10;
                    break;
                case 0x7000000:
                    score[0]=6;
                    score[4]=-6;
                    break;
                case 0x6000000:
                    score[0]=5;
                    score[4]=-5;
                    break;
                case 0x5000000:
                    score[0]=4;
                    score[4]=-4;
                    break;
                case 0x4000000:
                    score[0]=3;
                    score[4]=-3;
                    break;
                case 0x3000000:
                    score[0]=3;
                    score[4]=-3;
                    break;
            }
        }else if (myScore < yourScore) {
            switch (yourScore) {
                case 0xf000000:
                    score[4] = 108;
                    score[0] = -108;
                    break;
                case 0xe000000:
                    score[4] = 36;
                    score[0] = -36;
                    break;
                case 0xd000000:
                    score[4] = 24;
                    score[0] = -24;
                    break;
                case 0xc000000:
                    score[4] = 22;
                    score[0] = -22;
                    break;
                case 0xb000000:
                    score[4] = 20;
                    score[0] = -20;
                    break;
                case 0xa000000:
                    score[4] = 15;
                    score[0] = -15;
                    break;
                case 0x9000000:
                    score[4] = 12;
                    score[0] = -12;
                    break;
                case 0x8000000:
                    score[4] = 10;
                    score[0] = -10;
                    break;
                case 0x7000000:
                    score[4] = 6;
                    score[0] = -6;
                    break;
                case 0x6000000:
                    score[4] = 5;
                    score[0] = -5;
                    break;
                case 0x5000000:
                    score[4] = 4;
                    score[0] = -4;
                    break;
                case 0x4000000:
                    score[4] = 3;
                    score[0] = -3;
                    break;
                case 0x3000000:
                    score[4] = 3;
                    score[0] = -3;
                    break;
            }
        }else{//普通牌型判定
            //头道：
            if(myScore1==yourScore1){

            }else if(myScore1>yourScore1 && myScore1>=0x0300000) {//克子情况
                score[1]=3;
                score[5]=-3;
            }else if(myScore1<yourScore1 && yourScore1>=0x0300000) {//克子情况
                score[1]=-3;
                score[5]=3;
            }else if(myScore1>yourScore1) {//非克子情况
                score[1]=1;
                score[5]=-1;
            }else if(myScore1<yourScore1) {//非克子情况
                score[1]=-1;
                score[5]=1;
            }

            //第二道：
            if(myScore2==yourScore2){

            }else if(myScore2>yourScore2){
                if(myScore2>=0x0800000) {//同花顺
                    score[2]=10;
                    score[6]=-10;
                }else if(myScore2>=0x0700000){//炸弹
                    score[2]=8;
                    score[6]=-8;
                }else if(myScore2>=0x0600000){//葫芦
                    score[2]=3;
                    score[6]=-3;
                }else{
                    score[2]=1;
                    score[6]=-1;
                }
            }else {
                if(yourScore2>=0x0800000) {//同花顺
                    score[2]=-10;
                    score[6]=10;
                }else if(yourScore2>=0x0700000){//炸弹
                    score[2]=-8;
                    score[6]=8;
                }else if(yourScore2>=0x0600000){//葫芦
                    score[2]=-3;
                    score[6]=3;
                }else{
                    score[2]=-1;
                    score[6]=1;
                }
            }
            //第三道：
            if(myScore3==yourScore3){

            }else if(myScore3>yourScore3){
                if(myScore3>=0x0800000) {//同花顺
                    score[3]=5;
                    score[7]=-5;
                }else if(myScore3>=0x0700000){//炸弹
                    score[3]=4;
                    score[7]=-4;
                } else{
                    score[3]=1;
                    score[7]=-1;
                }
            }else {
                if(yourScore3>=0x0800000) {//同花顺
                    score[3]=-5;
                    score[7]=5;
                }else if(yourScore3>=0x0700000){//炸弹
                    score[3]=-4;
                    score[7]=4;
                }else{
                    score[3]=-1;
                    score[7]=1;
                }
            }
        }
        return score;
    }

    ////////特殊牌型判定////////////////////////////////////
    boolean kingDragon(Card[] group) {//顺子、同花
        int sf = sortFlush(group, false);
        if (sf == 0x000d || sf == 0x00d0 || sf == 0x0d00 || sf == 0xd000) {
            if (dragon(group)) {
                return true;
            }
        }
        return false;
    }//至尊清龙

    boolean dragon(Card[] group) {//顺子
        sortStraight(group, false);
        for (int i = 0; i < group.length - 1; i++) {
            if (group[i].getPoint() != group[i + 1].getPoint() - 1) {
                return false;
            }
        }
        return true;
    }//清龙

    boolean royal(Card[] group) {//顺子
        sortStraight(group, true);
        if (group[0].getPoint() == 11 && group[12].getPoint() == 14) {
            return true;
        }
        return false;

    }//十二皇族

    boolean threeStraightFlush(Card[] group) {//同花
        int sf = sortFlush(group, true);//已完成同花排序，返回值0x5035
        int i = 0;
        boolean largeOK = true;
        if (sf == 0x3550 || sf == 0x3505 || sf == 0x3055 || sf == 0x0355 || sf == 0x5350 || sf == 0x5305
                || sf == 0x5035 || sf == 0x0535 || sf == 0x5530 || sf == 0x5503 || sf == 0x5053 || sf == 0x0553) {
            sortStraight(group, true, 0, sf / 0x1000 - 1);
            for (i = 0; i < sf / 0x1000 - 1; i++) {
                if (group[i].getPoint() != group[i + 1].getPoint() - 1) {
                    largeOK = false;
                }
            }
            for (i = sf / 0x1000; i < sf / 0x1000 + sf % 0x1000 / 0x100 - 1; i++) {
                if (group[i].getPoint() != group[i + 1].getPoint() - 1) {
                    largeOK = false;
                }
            }
            for (i = sf / 0x1000 + sf % 0x1000 / 0x100; i < sf / 0x1000 + sf % 0x1000 / 0x100 + sf % 0x100 / 0x10 - 1; i++) {
                if (group[i].getPoint() != group[i + 1].getPoint() - 1) {
                    largeOK = false;
                }
            }
            for (i = sf / 0x1000 + sf % 0x1000 / 0x100 + sf % 0x100 / 0x10; i < sf / 0x1000 + sf % 0x1000 / 0x100 + sf % 0x100 / 0x10 + sf % 0x10 - 1; i++) {
                if (group[i].getPoint() != group[i + 1].getPoint() - 1) {
                    largeOK = false;
                }
            }
            if (largeOK) {
                return true;
            }
        }


        sf = sortFlush(myCards, false);
        if (sf == 0x3550 || sf == 0x3505 || sf == 0x3055 || sf == 0x0355 || sf == 0x5350 || sf == 0x5305
                || sf == 0x5035 || sf == 0x0535 || sf == 0x5530 || sf == 0x5503 || sf == 0x5053 || sf == 0x0553) {
            sortStraight(myCards, false, 0, sf / 0x1000 - 1);
            for (i = 0; i < sf / 0x1000 - 1; i++) {
                if (myCards[i].getPoint() != myCards[i + 1].getPoint() - 1) {
                    return false;
                }
            }
            for (i = sf / 0x1000; i < sf / 0x1000 + sf % 0x1000 / 0x100 - 1; i++) {
                if (myCards[i].getPoint() != myCards[i + 1].getPoint() - 1) {
                    return false;
                }
            }
            for (i = sf / 0x1000 + sf % 0x1000 / 0x100; i < sf / 0x1000 + sf % 0x1000 / 0x100 + sf % 0x100 / 0x10 - 1; i++) {
                if (myCards[i].getPoint() != myCards[i + 1].getPoint() - 1) {
                    return false;
                }
            }
            for (i = sf / 0x1000 + sf % 0x1000 / 0x100 + sf % 0x100 / 0x10; i < sf / 0x1000 + sf % 0x1000 / 0x100 + sf % 0x100 / 0x10 + sf % 0x10 - 1; i++) {
                if (myCards[i].getPoint() != myCards[i + 1].getPoint() - 1) {
                    return false;
                }
            }
            return true;
        }
        return false;

    }//三同花顺

    boolean threeBomb(Card[] group) {//顺子
        if (pairs(group, 4)[0] == 3) {
            return true;
        }
        return false;
    }//三炸弹

    boolean allLarge(Card[] group) {//顺子
        sortStraight(group, true);
        if (group[0].getPoint() >= 8 && group[12].getPoint() <= 14) {
            return true;
        }
        return false;
    }//全大

    boolean allSmall(Card[] group) {//顺子
        sortStraight(group, false);
        if (group[0].getPoint() >= 1 && group[12].getPoint() <= 8) {
            return true;
        }
        return false;
    }//全小

    boolean allRed(Card[] group) {//同花
        int sf = sortFlush(group, false);
        if (sf / 0x1000 == 0 && sf % 0x100 / 0x10 == 0) {
            return true;
        }
        return false;
    }//全红

    boolean allBlack(Card[] group) {//同花
        int sf = sortFlush(group, false);
        if (sf % 0x1000 / 0x100 == 0 && sf % 0x10 == 0) {
            return true;
        }
        return false;
    }//全黑

    boolean fourTOAK(Card[] group) {//顺子
        if (pairs(group, 3)[0] == 4) {
            return true;
        }
        return false;
    }//4克子

    boolean five(Card[] group) {//顺子
        if (pairs(group, 2)[0] == 5 && pairs(group, 3)[0] == 1) {
            return true;
        }
        return false;
    }//4对加葫芦(5对1克)

    boolean sixAndHalf(Card[] group) {//顺子
        if (pairs(group, 2)[0] == 6) {
            return true;
        }
        return false;
    }//六对半

    boolean threeFlush(Card[] group) {
        int sf = sortFlush(group, true);
        if (sf == 0x3550 || sf == 0x3505 || sf == 0x3055 || sf == 0x0355 || sf == 0x5350 || sf == 0x5305
                || sf == 0x5035 || sf == 0x0535 || sf == 0x5530 || sf == 0x5503 || sf == 0x5053 || sf == 0x0553
                || sf == 0x8300|| sf == 0x8030|| sf == 0x8003|| sf == 0x0830|| sf == 0x0803|| sf == 0x0083
                || sf == 0x3800|| sf == 0x3080|| sf == 0x3008|| sf == 0x0380|| sf == 0x0308|| sf == 0x0038) {
            return true;
        }
        return false;
    }//三同花

    boolean threeStraight(Card[] group) {
        boolean[] used = new boolean[13];
        for (int i = 0; i < used.length; i++) {
            used[i] = false;
        }

        sortStraight(group, false);
        int counter = 0;//计数
        int i = 0, j = 0;//i为当前牌序号，j为上一张牌序号
        boolean set = true;
        //355
        if (set) {
            for (i = 0; counter < 2; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 2) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        if (set) return true;

        //535
        for (i = 0; i < used.length; i++) {
            used[i] = false;
        }
        set = true;
        j = 0;
        counter = 0;
        if (set) {
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 2; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 2) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        if (set) return true;

        //553
        for (i = 0; i < used.length; i++) {
            used[i] = false;
        }
        set = true;
        j = 0;
        counter = 0;
        if (set) {
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 2; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 2) used[i] = true;
        }
        if (set) return true;

        sortStraight(group, true);
        counter = 0;//计数
        i = 0;
        j = 0;//i为当前牌序号，j为上一张牌序号
        set = true;
        //355
        if (set) {
            for (i = 0; counter < 2; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 2) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        if (set) return true;

        //535
        for (i = 0; i < used.length; i++) {
            used[i] = false;
        }
        set = true;
        j = 0;
        counter = 0;
        if (set) {
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 2; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 2) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        if (set) return true;

        //553
        for (i = 0; i < used.length; i++) {
            used[i] = false;
        }
        set = true;
        j = 0;
        counter = 0;
        if (set) {
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 4; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 4) used[i] = true;
        }
        counter = 0;
        if (set) {
            for (i = 0; used[i]; i++) j++;
            for (i = 0; counter < 2; i++) {
                if (group[i].getPoint() == group[j].getPoint()) continue;
                if (used[i]) continue;
                if (group[i].getPoint() == group[j].getPoint() + 1) {
                    used[j] = true;
                    counter++;
                    j = i;
                } else {
                    set = false;
                    break;
                }
            }
            if (counter == 2) used[i] = true;
        }
        if (set) return true;
        return false;
    }//三顺子

    ////////普通牌型判定////////////////////////////////////


    void shuffle() {
        Random random = new Random();
        int start = 0, end = 0;
        Card temp = null;
        for (int i = 0; i < 3000; i++) {
            start = random.nextInt(52);
            end = random.nextInt(52);
            temp = card[start];
            card[start] = card[end];
            card[end] = temp;

        }
    }//随机从牌堆中抽两张牌替换，反复1000次，实现洗牌

    /////////辅助处理方法///////////////////////////////////
    void sortStraight(Card[] group, boolean ALarge, int start, int end) {
        if (ALarge) {
            for (int i = start; i <= end; i++) {
                if (group[i].getPoint() == 1) group[i].setPoint(14);
            }
        } else {
            for (int i = start; i <= end; i++) {
                if (group[i].getPoint() == 14) group[i].setPoint(1);
            }
        }//预处理

        for (int i = start; i <= end - 1; i++) {
            for (int k = start; k <= end - 1 - i; k++) {
                if (group[k].getSuit() + 16 * group[k].getPoint() > group[k + 1].getSuit() + 16 * group[k + 1].getPoint()) {
                    Card t = group[k];
                    group[k] = group[k + 1];
                    group[k + 1] = t;
                }
            }
        }//冒泡排序算法

    }//按顺子排序

    void sortStraight(Card[] group, boolean ALarge) {
        if (ALarge) {
            for (int i = 0; i < group.length; i++) {
                if (group[i].getPoint() == 1) group[i].setPoint(14);
            }
        } else {
            for (int i = 0; i < group.length; i++) {
                if (group[i].getPoint() == 14) group[i].setPoint(1);
            }
        }//预处理

        for (int i = 0; i < group.length - 1; i++) {
            for (int k = 0; k < group.length - 1 - i; k++) {
                if (group[k].getSuit() + 16 * group[k].getPoint() > group[k + 1].getSuit() + 16 * group[k + 1].getPoint()) {
                    Card t = group[k];
                    group[k] = group[k + 1];
                    group[k + 1] = t;
                }
            }
        }//冒泡排序算法

    }//按顺子排序

    int sortFlush(Card[] group, boolean ALarge) {
        if (ALarge) {
            for (int i = 0; i < group.length; i++) {
                if (group[i].getPoint() == 1) group[i].setPoint(14);
            }
        } else {
            for (int i = 0; i < group.length; i++) {
                if (group[i].getPoint() == 14) group[i].setPoint(1);
            }
        }//预处理

        int a = 0, b = 0, c = 0, d = 0;
        for (int i = 0; i < group.length; i++) {
            switch (group[i].getSuit()) {
                case 1:
                    a++;
                    break;
                case 2:
                    b++;
                    break;
                case 3:
                    c++;
                    break;
                case 4:
                    d++;
                    break;
            }
        }//统计花色个数

        for (int i = 0; i < group.length - 1; i++) {
            for (int k = 0; k < group.length - 1 - i; k++) {
                if (16 * group[k].getSuit() + group[k].getPoint() > 16 * group[k + 1].getSuit() + group[k + 1].getPoint()) {
                    Card t = group[k];
                    group[k] = group[k + 1];
                    group[k + 1] = t;
                }
            }
        }//冒泡排序算法

        return a * 0x1000 + b * 0x100 + c * 0x10 + d;
    }//按花色排序

    int[] pairs(Card[] group, int repeat) {
        sortStraight(group, true);
        //sortStraight(group, false);
        int[] pair = new int[15];
        for (int i = 0; i < 15; i++) {
            pair[i] = 0;
        }

        int t = 0;
        for (int i = 0; i < group.length; i++) {
            t = group[i].getPoint();
            //Log.i("pairs",t+"");
            pair[t]+=1;
        }
        for (int i = 1; i < 15; i++) {
            if (pair[i] == repeat) {
                pair[0]++;
            }
        }

        return pair;
    }//判断对子数，返回对子数和各点数牌的数量

}

